const err = require("../constant/err.type.js");
const { Gzgj, Poi, Updata, Nodes } = require("../common/file.js");
const { DATA_PATH } = require("../config/config.default.js");
const { getGaojing } = require("../common/common.js");
const {saveGaojings,getGaojings,getTerminalGaojing} = require("../services/gaojing.service.js");
const {saveLinks,getLinkByName} = require("../services/links.service.js");
const gzgj = new Gzgj(`${DATA_PATH}Js_GZGJ.json`);
const poi = new Poi(`${DATA_PATH}Js_POI.json`);
const updata = new Updata(`${DATA_PATH}Js_UpData.json`);
class Files {
    //刷新文件
    async updateJsonFile(){
        updata.writeFile({ num: 1 })
        setTimeout(() => {
            updata.writeFile({ num: 0 })
        }, 500);
        setTimeout(() => {
            updata.writeFile({ num: 0 })
        }, 1000);
        ctx.app.emit("error",err.setDefaultValue({code:0,msg:"ok",data:""}),ctx);
    }
    //创建和保存变电站
    async saveBDZ(ctx,next){
        try {
            var res;
            var {...station} = ctx.request.body;
            console.log("station  =>",station);
            res = poi.insertBDZ({value:station});
            ctx.app.emit("error",err.setDefaultValue({code:0,msg:"ok",data:""}),ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //导入节点时，创建Js_node.json
    async jsonImportOpticals(ctx, next) {
        try {
            var res;
            var { linkId, linkName, arr } = ctx.request.body;
            if (linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取JSON文件名编号失败", data: "" }), ctx);
                return;
            }
            res = Nodes.createLine({ linkId, linkName, arr });
            if (res) {
                updata.writeFile({ num: 1 });
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入JSON成功", data: "" }), ctx);
                return;
            } else {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "写入JSON失败", data: "" }), ctx);
                return;
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //删除指定JS_Node.json文件
    async jsonEraseNodeJs(ctx, next) {
        try {
            var res;
            var { linkId } = ctx.request.body;
            if (!linkId || linkId < 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取JSON文件名编号失败", data: "" }), ctx);
                return;
            }
            res = Nodes.eraseFile(linkId);
            if (res) {
                updata.writeFile({ num: 1 });
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "删除JSON成功", data: "" }), ctx);
                return;
            } else {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "删除JSON失败", data: "" }), ctx);
                return;
            }

        } catch (error) {
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //设置故障告警
    async setAlarm(ctx, next) {
        try {
            var res;
            var { list, gaojing } = ctx.request.body;
            if (!list || !(list instanceof Array) || list.length == 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取光缆信息失败", data: "" }), ctx);
                return;
            }
            if (gaojing.linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取JSON文件名编号失败", data: "" }), ctx);
                return;
            }
            if (gaojing.linkName == "" || !gaojing.linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取线路名称失败", data: "" }), ctx);
                return;
            }
            if (gaojing.status == "" || !gaojing.status) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取线路状态失败", data: "" }), ctx);
                return;
            }
            if (gaojing.statusValue == "" || !gaojing.statusValue) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取线路状态值失败", data: "" }), ctx);
                return;
            }
            //计算告警信息
            res = getGaojing(list, gaojing.statusValue);
            let url = ctx.request.host
            if (res.code == 0) {
                let point = { ID: `GZGJ${gaojing.gaojingId}`, Class: "GZ", Name: `BP_POI_GZGJ${gaojing.gaojingId}`, Show_Name: gaojing.linkName, Mesh: "SM_GZGJ", X: res.data.x, Y: res.data.y, Z: "8000", URL: `http://${url}/gzgj?gaojingId=${gaojing.gaojingId}`, Build: "1", Visible: "1", Click: "1", Init: "0", R: "1", G: "0", B: "0", Color: "" };
                
                let as = poi.setGaojing({ values: point });
                let wr = gzgj.setValue({ values: point });
                if (!wr) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "生成Js_GZGJ失败", data: "" }), ctx);
                    return;
                }
                //poi.writeFile();
                //改变线路颜色代码，需要linkId,linkName,originId
                Nodes.changeColor({linkId:gaojing.linkId,linkName:gaojing.linkName,num:res.data.originId,model:2});
                updata.writeFile({ num: 1 });
                setTimeout(() => {
                    point.Click = "0";
                    poi.setGaojing({ values: point });
                    gzgj.setValue({ values: point })
                    //poi.writeFile();
                    updata.writeFile({ num: 1 })
                }, 500);
                setTimeout(() => {
                    point.Click = "0";
                    poi.setGaojing({ values: point });
                    gzgj.setValue({ values: point })
                    //poi.writeFile();
                    updata.writeFile({ num: 1 })
                }, 1000);
                ctx.status = 200;
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
                return;
            } else {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "计算告警信息时错误", data: "" }), ctx);
                return;
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "计算告警信息时错误", data: "" }), ctx);
            return;
        }
    }
    //故障定位 
    async dingwei(ctx, next) {
        try {
            var { linkName, gaojingId } = ctx.request.body;
            if (!gaojingId || gaojingId == undefined || gaojingId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "告警ID错误", data: "" }), ctx);
                return;
            }
            if(!linkName || linkName == undefined || linkName == ""){
                ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"线路名称错误",data:""}),ctx);
                return;
            }
            //let gaojing = gzgj.getValueByCondition({ key: "ID", value: `GZGJ${gaojingId}` });
            let gaojing = gzgj.getValueByCondition({ key: "Show_Name", value: linkName });
            if (!gaojing) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            gaojing.Click = "1";
            let pbj = poi.getValueByCondition({ item: "gaojings", key: "Show_Name", value: linkName  });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_POI.json失败", data: "" }), ctx);
                return;
            }
            pbj.Click = "1";
            poi.setGaojing({ vlaues: pbj });
            gzgj.setValue({ values: gaojing });
            //poi.writeFile();
            setTimeout(() => {
                gaojing.Click = "0";
                pbj.Click = "0"
                poi.setGaojing({ vlaues: pbj });
                gzgj.setValue({ values: gaojing });
                //poi.writeFile();
            }, 500)
            setTimeout(() => {
                gaojing.Click = "0";
                pbj.Click = "0"
                poi.setGaojing({ vlaues: pbj });
                gzgj.setValue({ values: gaojing });
                //poi.writeFile();
            }, 1000)
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: error, data: "" }), ctx);
            return;
        }
    }
    //解除告警
    async jiechu(ctx, next) {
        try {
            var { linkId,linkName, gaojingId } = ctx.request.body;
            if (!gaojingId || gaojingId == undefined || gaojingId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "告警ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName || linkName == undefined || linkName == "") {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let gaojing = gzgj.getValueByCondition({key: "Show_Name", value: linkName });
            if (!gaojing) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            let pbj = poi.getValueByCondition({ item: "gaojings", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_POI.json失败", data: "" }), ctx);
                return;
            }
            gaojing['Init'] = "1";
            gaojing['X'] = "0";
            gaojing['Y'] = "0";
            gaojing['Z'] = "8000000";
            pbj['Init'] = "1";
            pbj['X'] = "0";
            pbj['Y'] = "0";
            pbj['Z'] = "8000000";
            poi.setGaojing({ vlaues: pbj });
            gzgj.setValue({ values: gaojing });
            //poi.writeFile();
            setTimeout(() => {
                gaojing['Init'] = "0";
                pbj['Init'] = "0";
                poi.setGaojing({ vlaues: pbj});
                gzgj.setValue({ values: gaojing });
                //poi.writeFile();
            }, 500);//解除故障时，0.5秒后init就成0
            setTimeout(() => {
                gaojing['Init'] = "0";
                pbj['Init'] = "0";
                poi.setGaojing({ vlaues: pbj});
                gzgj.setValue({ values: gaojing });
                //poi.writeFile();
            }, 1000);//解除故障时，0.5秒后init就成0
            //改变线路颜色代码，需要linkId,linkName,originId
            Nodes.changeColor({linkId,linkName,num:0,model:2});
            updata.writeFile({ num: 1 })
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //重现告警
    async createGzcx(ctx,next){
        try {
            var {...gaojing} = ctx.request.body;
            gaojing.Click = "1";
            poi.setGzcx(gaojing);
            setTimeout(() => {
                gaojing.Click = "0";
                poi.setGzcx(gaojing);
            },500);
            updata.writeFile({ num: 1 })
            setTimeout(() => {
                gaojing.Click = "0";
                poi.setGzcx(gaojing);
            },1000);
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //清除重现告警
    async clearGzcx(ctx,next){
        try {
            var {...gaojing} = ctx.request.body;
            gaojing.Init = "1";
            poi.setGzcx(gaojing);
            setTimeout(() => {
                gaojing.Init = "0";
                poi.setGzcx(gaojing);
            },500);
            updata.writeFile({ num: 1 })
            setTimeout(() => {
                gaojing.Init = "0";
                poi.setGzcx(gaojing);
            },1000);
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //在Js_POI.json创建line
    async jsonCreateLine(ctx, next) {
        try {
            var { linkId, linkName, arr } = ctx.request.body;
            if (!linkId) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            if (!(arr instanceof Array) || arr.length < 2) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "没有可添加的节点", data: "" }), ctx);
                return;
            }
            var len = arr.length;
            let url = ctx.request.host;
            let num = len % 2 == 0 ? len / 2 - 1 : Math.floor(len / 2) - 1;
            let x = 3836187.737 * (0 + arr[num]['longitude'] - 111.67240) / 0.86915612;
            let y = -7091297.591 * (0 + arr[num]['latitude'] - 40.79964) / 1.20283173;
            let R = arr[num]['R'];
            let G = arr[num]['G'];
            let B = arr[num]['B'];
            let obj = { ID: `Line${linkId}`, Class: "Line", Name: `BP_POI_Line${linkId}`, Show_Name: linkName, Mesh: "SM_Line", X: x.toString(), Y: y.toString(), Z: "5000", URL: `http://${url}/line?linkId=${linkId}`, Build: "1", Visible: "1", Click: "0", Init: "0", Color: "Green", R: R, G: G, B: B }
            /* let pbj = poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            } */
            poi.setLines({ values: obj, key: "Show_Name" });
            //poi.writeFile();
            updata.writeFile({ num: 1 });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //删除坐标信息时，修改POI
    async lineEraseNode(ctx, next) {
        try {
            var { linkId, linkName } = ctx.request.body;
            if (!linkId) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let pbj = poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            pbj['Build'] = "0";
            poi.setLines({ values: pbj, key: "Show_Name" });
            //poi.writeFile();
            updata.writeFile({ num: 1 });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //删除线时，修改POI
    async lineEraseLink(ctx, net) {
        try {
            var { linkId, linkName } = ctx.request.body;
            if (!linkId) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let pbj = poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            pbj['Init'] = "1";
            pbj['X'] = "0";
            pbj['Y'] = "0";
            pbj['Z'] = "8000";
            poi.setLines({ values: pbj, key: "Show_Name" });
            //poi.writeFile();
            updata.writeFile({ num: 1 });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //gllist页面点击线路修改visible和click属性
    async hitLine(ctx, next) {
        try {
            var res;
            var { linkId, linkName } = ctx.request.body;
            if (linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let pbj = poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            console.log("pbj=>",pbj);
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_POI.json失败", data: "" }), ctx);
                return;
            }
            poi.hitLines({ values: { Show_Name: linkName }, key: "Show_Name" });
            setTimeout(() => {
                pbj.Click = "0";
                poi.setLines({ values: pbj, key: "Show_Name" });
                //poi.writeFile();
            }, 500)
            setTimeout(() => {
                pbj.Click = "0";
                poi.setLines({ values: pbj, key: "Show_Name" });
                //poi.writeFile();
            }, 1000)
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //设置poi.josn中的show_name
    async setShowName(ctx, next) {
        try {
            var res;
            var { linkId, linkName } = ctx.request.body;
            if (!linkId || linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "linkId错误", data: "" }), ctx);
                return;
            }
            if (!linkName || linkName <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let pbj = poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;

        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    async autoGzgj(ctx, netx) {
        try {
            var obj = ctx.request.body;
            if (!obj.linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "参数错误", data: obj }), ctx);
                return;
            }
            if (!obj.alarmType || obj.alarmType < 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "告警类型编码错误", data: obj.alarmType }), ctx);
                return;
            }
            if (!obj.alarmDetails || obj.alarmDetails <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "告警详细内容错误", data: obj.alarmDetails }), ctx);
                return;
            }
            // if(obj.alarmDescribe.衰减值 >= 0){
            //   ctx.app.emit("error",err.setDefaultValue({code:0,msg:"衰减值错误",data:obj.alarmDetails}),ctx);
            //   return;
            // }
            // if(!obj.alarmDescribe.告警描述){
            //   ctx.app.emit("error",err.setDefaultValue({code:0,msg:"告警描述错误",data:obj.alarmDetails}),ctx);
            //   return;
            // } */
            // /* if(!obj.alarmDescribe.position){
            //   ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"告警长度错误",data:obj.alarmDetails}),ctx);
            //   return;
            // }
            if (!obj.alarmDescribe.latitude) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "纬度错误", data: obj.alarmDetails }), ctx);
                return;
            }
            if (!obj.alarmDescribe.longitude) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "经度错误", data: obj.alarmDetails }), ctx);
                return;
            }
            let lk = await getLinkByName(obj.linkName);
            if (!lk) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: '查找链路失败', data: "" }), ctx)
            }
            let data = {
                linkName: lk.linkName,
                linkId: lk.linkId,
                Longitude: obj.alarmDescribe.longitude,
                Latitude: obj.alarmDescribe.latitude,
                lineName: lk.lineName,
                times: lk.times,
                cancat: lk.cancat,
                works: lk.works
            }
            if (obj.alarmType == 50 || obj.alarmType == 6 || obj.alarmType == 54) {
                data.status = "中断";
            } else if (obj.alarmType == 51 || obj.alarmType == 53 || obj.alarmType == 56 || obj.alarmType == 57) {
                data.status = "正常"
            } else if (obj.alarmType == 52) {
                data.status = "衰减"
            } else {
                //data.status = "中断"
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "故障类型识别错误", data: obj.alarmType }), ctx);
                return;
            }
            if (lk.status == data.status) {
                if (obj.alarmDescribe.position > 0) {
                    //存在arlarmDescribe.position时，代表是断路或衰减，这时候，如果position不同的时候，继续报警，如果相同，则不报警
                    if (obj.alarmDescribe.position == lk.statusValue) {
                        ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "针对本线路操作重复，忽略此操作", data: obj.alarmType }), ctx);
                        return;
                    }
                } else {
                    ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "针对本线路操作重复，忽略此操作", data: obj.alarmType }), ctx);
                    return;
                }
            }
            if (obj.alarmDescribe.position) {
                data.statusValue = obj.alarmDescribe.position;
            }
            var link = await getLinkByName(data.linkName);
            if (data.status == "正常") {
                //解除报警
                let gj = getTerminalGaojing({linkName:data.linkName,gaojingType:1});
                if(!gj){
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "没有找到可以匹配的告警数据", data: "" }), ctx);
                    return;
                }
                //保存告警信息
                let tmp = saveGaojings({goajingId:gj.gaojingId,gaojingType:1,status:data.status,statusValue:data.statusValue});
                if(!tmp){
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存告警信息失败", data: "" }), ctx);
                    return;
                }
                let gaojing = gzgj.getValueByCondition({key: "Show_Name", value: data.linkName });
                if (!gaojing) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                    return;
                }
                let pbj = poi.getValueByCondition({ item: "gaojings",key: "Show_Name", value: data.linkName });
                if (!pbj) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                    return;
                }
                gaojing['Init'] = "1";
                gaojing['X'] = "0";
                gaojing['Y'] = "Y";
                gaojing['Z'] = "8000000";
                pbj['Init'] = "1";
                pbj['X'] = "0";
                pbj['Y'] = "Y";
                pbj['Z'] = "8000000";
                poi.setGaojing({ vlaues: pbj, key: "ID" });
                gzgj.setValue({ values: gaojing });
                //poi.writeFile();
                setTimeout(() => {
                    gaojing['Init'] = "0";
                    pbj['Init'] = "0";
                    poi.setGaojing({ vlaues: pbj, key: "ID" });
                    gzgj.setValue({ values: gaojing });
                    //poi.writeFile();
                }, 500);//解除故障时，0.5秒后init就成0
                setTimeout(() => {
                    gaojing['Init'] = "0";
                    pbj['Init'] = "0";
                    poi.setGaojing({ vlaues: pbj, key: "ID" });
                    gzgj.setValue({ values: gaojing });
                    //poi.writeFile();
                }, 1000);//解除故障时，0.5秒后init就成0
                if (link instanceof Object) {
                    let ss = await saveLinks({ linkId: link.linkId, status: "正常", statusValue: "" });
                    if (!ss) {
                        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存线路信息失败", data: "" }), ctx);
                        return;
                    }
                    //await changeColor({ linkId: link.linkId, linkName: link.linkName }, 0, 2);
                    //改变线路颜色代码，需要linkId,linkName,originId
                    Nodes.changeColor({linkId:link.linkId,linkName:link.linkNme,num:0,model:2})
                    updata.writeFile({ num: 1 })
                } else {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存线路信息失败", data: "" }), ctx);
                    return;
                }
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
            } else {
                //设置报警
                var res;
                if (!data.linkName) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误 ", data: "" }), ctx);
                    return;
                }
                let pd = { linkId: link.linkId, status: data.status, statusValue: data.statusValue }
                let ss = await saveLinks(pd);
                if (!ss) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存线路信息失败1", data: "" }), ctx);
                    return;
                }
                var rs = await getOptical({ linkName: data.linkName });
                if (!rs || !(rs instanceof Array) || rs.length == 0) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取坐标信息错误", data: "" }), ctx);
                    return;
                }
                let list = rs;
                //计算告警信息
                res = getGaojing(list, data.statusValue);
                let url = ctx.request.host;
                if (res.code == 0) {
                    let point = { ID: `GZGJ1`, Class: "GZ", Name: `BP_POI_GZGJ1`, Show_Name: link.linkName, Mesh: "SM_GZGJ", X: res.data.x, Y: res.data.y, Z: "8000", URL: `http://${url}/gzgj?linkId=${link.linkId}`, Build: "1", Visible: "1", Click: "1", Init: "0", R: "1", G: "0", B: "0", Color: "" }
                    //设备告警信息
                    let obj = { type: res.data.type, origin: res.data.origin, finishing: res.data.finishing, finishingId: res.data.finishingId, distance: res.data.distance, X: res.data.x, Y: res.data.y, linkId: link.linkId, linkName: link.linkName, lineName: link.lineName, times: link.times, status: data.status, statusValue: data.statusValue, cancat: link.cancat, works: link.works };
                    let gjdata = {origin: res.data.origin, finishing: res.data.finishing, finishingId: res.data.finishingId, distance: res.data.distance, X: res.data.x, Y: res.data.y, linkId: link.linkId, linkName: link.linkName, lineName: link.lineName, times: link.times, status: data.status, statusValue: data.statusValue, cancat: link.cancat, works: link.works}
                    let gjrs = saveGaojings(gjdata);
                    if(!gjrs){
                        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存告警信息失败", data: "" }), ctx);
                        return;
                    }
                    
                    let ps = poi.setGaojing({ values: point });
                    //poi.writeFile();
                    if(!ps){
                        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "生成Js_POI.json失败", data: "" }), ctx);
                        return;
                    }  
                    let wr = gzgj.setValue({ values: point });
                    if (!wr) {
                        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "生成Js_GZGJ.json失败", data: "" }), ctx);
                        return;
                    }                  
                    //填写改变颜色代码
                    Nodes.changeColor({linkId:link.linkId,linkName:link.linkNme,num:res.data.originId,model:1})
                    updata.writeFile({ num: 1 });
                    setTimeout(()=>{
                        point.Click = "0";
                        poi.setGaojing({ values: point });
                        gzgj.setValue({ values: point });
                        //poi.writeFile();
                        updata.writeFile({ num: 1 });
                    },500)
                    setTimeout(()=>{
                        point.Click = "0";
                        poi.setGaojing({ values: point });
                        gzgj.setValue({ values: point });
                        //poi.writeFile();
                        updata.writeFile({ num: 1 });
                    },1000)
                    ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入JSON成功", data: "" }), ctx);
                    return;
                } else {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: res.msg, data: res.data }), ctx);
                    return;
                }
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "未知错误", data: obj.alarmType }), ctx);
            return;
        }
    }
    async jsonCreateBOX(ctx,next){
        try {
            var {linkId,arr} = ctx.request.body;
            if(!linkId || linkId <= 0 ){
                ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"线路ID错误",data:""}),ctx);
                return;
            }
            if(!arr || !(arr instanceof Array) || arr.length == 0){
                ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"数据不能为空",data:""}),ctx);
                return;
            }
            poi.pushBox({arr:arr,linkId});
            ctx.app.emit("error",err.setDefaultValue({code:0,msg:"ok",data:""}),ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"创建box失败，"+ error,data:""}),ctx)
        }
    }
    async jsonEraseBOX(ctx,next){
        try {
            var {linkId} = ctx.request.body;
            if(!linkId || linkId <= 0 || linkId == undefined){
                ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"线路ID错误",data:""}),ctx);
                return;
            }
            poi.clearBox({linkId});
            ctx.app.emit("error",err.setDefaultValue({code:0,msg:"ok",data:""}),ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"创建box失败，"+ error,data:""}),ctx)
        }
    }
    async jsonCreateBDZ(ctx,next){
        try {
            var {arr} = ctx.request.body;
            /* if(!linkId || linkId <= 0 ){
                ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"线路ID错误",data:""}),ctx);
                return;
            } */
            if(!arr || !(arr instanceof Array) || arr.length == 0){
                ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"数据不能为空",data:""}),ctx);
                return;
            }
            poi.pushGDJ({values:arr,linkId});
            ctx.app.emit("error",err.setDefaultValue({code:0,msg:"ok",data:""}),ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"创建box失败，"+ error,data:""}),ctx)
        }
    }
    async jsonEraseBDZ(ctx,next){
        try {
            /* var {} = ctx.request.body;
            if(!linkId || linkId <= 0 || linkId == undefined){
                ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"线路ID错误",data:""}),ctx);
                return;
            } */
            poi.clearGDJ({linkId});
            ctx.app.emit("error",err.setDefaultValue({code:0,msg:"ok",data:""}),ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"创建box失败，"+ error,data:""}),ctx)
        }
    }
}

module.exports = new Files();